import pygtk
pygtk.require('2.0')
import gtk
import os

from mokocard.util.gtkutil import getScrolledWidget
from mokocard.util.sqlutil import SqlUtil

class DbStatsPage(gtk.VBox):
 
    def showCards(self, widget, filter):
        cards = self.sqlUtil.getFilterCards(filter)
        if cards == None:
            print "No database open or generic error"
            self.textview.get_buffer().set_text("No database open or generic error")
        else:
            strCards = ""
            for x in range(0,len(cards)):
                strCards= strCards + "Q: " + cards[x].question + "\n A: " + cards[x].answer + "\n"
            
            self.textview.get_buffer().set_text(strCards)
        
        cards = None
            
    def loadDatabaseStats(self, widget):
        dbStats = self.sqlUtil.getDbStats()
        if dbStats == None:
            print "No database open or generic error"
            self.textview.get_buffer().set_text("No database open or generic error")
        else:
            strStats = "Database Name: " + self.sqlUtil.pathDatabase + "\n"
            strStats = strStats + "Read Only: " + str(dbStats.getReadOnly()) + "\n"
            strStats = strStats + "Reverse: " + str(dbStats.getReverse()) + "\n"
            strStats = strStats + "Record counts: " + str(dbStats.getCountRecords()) + "\n"
            strStats = strStats + "Ok Answers: " + str(dbStats.getOkAnswers()) + "\n"
            strStats = strStats + "Ko Answers: " + str(dbStats.getKoAnswers()) + "\n\n"
            strStats = strStats + "Count answer/date:\n"

            dateCount = dbStats.getDateCount()
            
            for x in range(0,len(dateCount)):
                strStats = strStats + dateCount[x].getDate() + " - " + str(dateCount[x].getCount()) + "\n"
                
            self.textview.get_buffer().set_text(strStats)
        
    def build(self, configData):

        self.textview = gtk.TextView()
        self.textview.show()
        self.textview.set_sensitive(False)
        self.textview.set_editable(False)
        scrolledWidget = getScrolledWidget()
        scrolledWidget.add(self.textview)
        scrolledWidget.show()
        
        btLoadDatabase = gtk.Button("  Refresh  ")
        btLoadDatabase.connect("clicked", self.loadDatabaseStats)
        btLoadDatabase.set_size_request(configData.getButtonStatsWidth(), configData.getButtonStatsHeight())
        btLoadDatabase.show()

        btShowCards = gtk.Button("All Cards")
        btShowCards.connect("clicked", self.showCards, None)
        btShowCards.set_size_request(configData.getButtonStatsWidth(), configData.getButtonStatsHeight())
        btShowCards.show()
        
        koCards = gtk.Button("Ko Cards")
        koCards.connect("clicked", self.showCards, "KO")
        koCards.set_size_request(configData.getButtonStatsWidth(), configData.getButtonStatsHeight())
        koCards.show()
        
        hButtonBox = gtk.HButtonBox()
        hButtonBox.set_layout(gtk.BUTTONBOX_SPREAD)
        hButtonBox.pack_start(btLoadDatabase, False, False, 0)
        hButtonBox.pack_start(btShowCards, False, False, 0)
        hButtonBox.pack_start(koCards, False, False, 0)
        hButtonBox.show()
        
        self.pack_start(scrolledWidget, True, True, 10)
        self.pack_start(hButtonBox, False, False, 10)
    
    def __init__(self, configData, sqlUtil):

        self.sqlUtil = sqlUtil
        
        # ten pixels space
        gtk.VBox.__init__(self, False, 10)
       
        self.build(configData)
        self.show()